<template>
	<view class="content">
		<view class="header" v-if="false">
			<!-- v-for="(item, index) in list" -->
			<!-- :key="index" -->
			<!-- <zero-lazy-load
			    class="headerImg"
			    image="https://cdn.pixabay.com/photo/2020/05/19/13/32/cartoon-5190837_1280.jpg"
			    imgMode="aspectFill"
				height='406'
			  ></zero-lazy-load> -->
			<!-- <easy-loadimage mode="widthFix" class="headerImg" view-height='406' :scroll-top="0" loadingMode="spin-circle"
				image-src="https://cdn.pixabay.com/photo/2020/05/19/13/32/cartoon-5190837_1280.jpg"></easy-loadimage> -->
			<image src="https://cdn.pixabay.com/photo/2020/05/19/13/32/cartoon-5190837_1280.jpg" class="headerImg"
				mode="aspectFill" lazy-load></image>
			<view class="txtWrap">
				<image src="/static/index/Self-selected_direct_purchase.png" mode="aspectFit" class="pageTitleImg">
				</image>

			</view>
		</view>
		<view class="cardWrap">
			<view class="title">
				自选直购
			</view>
			<view class="centerBox">
<scroll-view scroll-y="true" style="height: calc(70vh - 330rpx);">
				<view class="pictureBox">
					
					<view class="pictureItem" v-for="(item,index) in list" :key="index">
						<cardBag :cardWidth='208' :cardHeight='208' :tagWidth='72' :cardData="item" :urlKey="'thumbnail'">
							<template #option>
								<view class="optionLay" @click="toggleSelection(item)">
									<!-- // :class="{ disabled: isDisabled(item.id) }" -->
									<uni-icons class="optionIcon" :type="isSelected(item.id)?'checkbox-filled':'circle'"
										color="#fff" size="14"></uni-icons>
									<!-- <uni-icons class="optionIcon" v-else type="circle" color="#fff"
										size="14"></uni-icons> -->
								</view>
							</template>
							<template #infoMore>
								<!-- 1:画师 2:扭赠 3:端盒)' -->
								<view class="infoMore" v-if="item.cat_type==1">
									<view class="priceTxt ">
										<!-- <text class="unit" >{{}}</text> -->
										画师：{{item.cat_name}}
									</view>
									<!-- <view class="priceTxt forward">
										<view class="txt">
											转发价
										</view>
										<text class="unit">￥</text>
										200
									</view> -->

								</view>
								<view class="infoMore" v-else>
									<view class="priceTxt " v-if="item.cat_type==2">扭赠</view>
									<view class="priceTxt " v-if="item.cat_type==3">端盒</view>
								</view>
							</template>
						</cardBag>
					</view>
				</view>
					</scroll-view>

			</view>
		</view>
		<view class="playWrap">
			<!-- <view class="all" @click="allType=!allType">
				<uni-icons class="optionIcon" :type="allType?'checkbox-filled':'circle'" color="#3CC599"
					size="18"></uni-icons>
				全选
			</view> -->
			<view class="infoBox">
				<view class="infoTxt">
					<view class="lable">
						<!-- 购买2张 -->
					</view>
					<view class="value">
						<view class="lable">合计：</view>
						<view class="priceTxt ">
							<text class="unit">￥</text>
							{{machine.price}}
						</view>
					</view>
				</view>
			</view>
			<view class="bnt" @click="ordersCreateFn(undefined,'lottery_purchase')">
				结算
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				allType: false,
				list: [],
				shopId: '',
				machineId: '',
				machine: {},
				maxSelectCount: 0, //可以进行自选直购的张数,最大选择个数，
				selectedItems: [],
				selectedIds: [],
			};
		},
		onLoad(e) {
			this.shopId = e.shopId;
			this.machineId = e.machineId;
			this.machinesDetailFn();
		},
		filters: {

		},
		computed: {
			selectedMap() {
				const map = {};
				this.selectedIds.forEach(id => {
					map[id] = true;
				});
				return map;
			},
			isMaxReached() {
				return this.selectedIds.length >= this.maxSelectCount;
			}
		},
		methods: {
			refsLoad(shopId,machineId){
				console.log(shopId,
machineId,"什么,1");
				this.shopId = shopId;
				this.machineId = machineId;
				this.machinesDetailFn();
			},
			optionFN(item) {
				item.action = !item.action
			},
			// 通过ID获取机台详情
			machinesDetailFn() {
				this.$http('/v1/machines/detail/' + this.machineId).then(res => {
					console.log(res, "获取机台详情");
					if (res.code == 0) {
						const data = res.data;
						this.machine = data.machine;
						this.maxSelectCount = data.draw_to_direct_count||0;
						this.list = data.all_images.filter(item => {
							return item.parent_id == 0;
						});
						console.log(this.list, "this.list");
						this.selectedItems= [];
						this.selectedIds= [];
						// 只会显示parent_id==0的参数;
						// this.shopsDetail = res.data.shop;
						// this.is_followed = res.data.is_followed;
					} else {
						this.$showToast(res.msg)
					}
				})
			},
			toggleSelection(item) {
				if (this.maxSelectCount==0) {
					uni.showToast({
						title: `自选直购张数不足`,
						icon: 'none'
					});
					return;
				}
				if (this.isSelected(item.id)) {
					this.selectedIds = this.selectedIds.filter(id => id !== item.id);
					this.selectedItems = this.selectedItems.filter(selectedItem => selectedItem.id !== item.id);
					return;
				}

				if (this.isMaxReached) {
					uni.showToast({
						title: `最多只能选择${this.maxSelectCount}个项目`,
						icon: 'none'
					});
					return;
				}

				this.selectedIds.push(item.id);
				this.selectedItems.push({
					...item
				});
			},
			isSelected(id) {
				return !!this.selectedMap[id];
			},
			isDisabled(id) {
				return this.isMaxReached && !this.isSelected(id);
			},
			ordersCreateFn(num, type) {
				if (this.selectedIds.length === 0) {
					uni.showToast({
						title: '请至少选择一个',
						icon: 'none'
					});
					return;
				}
				// let newData = [this.list[0], this.list[1]]; //用户选择的图；
				console.log(this.selectedIds, this.selectedItems);
				uni.setStorageSync('userSelects', this.selectedItems)
				uni.setStorageSync('userSelectsIds', this.selectedIds)
				uni.navigateTo({
					url: `/pages/my/orderConfirmation?price=${this.machine.price}&playType=lottery_purchase&shopId=${this.shopId}&machineId=${this.machineId}`
				})
				this.$emit('results');
				return
				this.$http('/v1/orders/create', {
					"type": type,
					"shop_id": Number(this.shopId),
					"machine_id": Number(this.machineId),
					"num": num,
					images_ids: [256, 257]
				}, 'POST').then(res => {
					console.log(res);
					if (res.code == 0) {
						uni.navigateTo({
							url: `/pages/my/orderConfirmation?order_sn=${res.data.order_sn}`
						})
						return;
						let wechatParams = res.data.wechat_params
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: wechatParams.timeStamp,
							nonceStr: wechatParams.nonceStr,
							package: wechatParams.package,
							signType: wechatParams.signType,
							paySign: wechatParams.paySign,
							success: function(res) {
								console.log('success:' + JSON.stringify(res));
							},
							fail: function(err) {
								console.log('fail:' + JSON.stringify(err));
							}
						});

					} else {}
					this.$showToast(res.msg)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header {
		width: 100vw;
		height: 406upx;
		position: relative;

		::v-deep .headerImg {
			// image{
			@include setwh(100%, 100%);
			// }
		}

		.txtWrap {
			position: absolute;
			bottom: 86upx;
			left: 50upx;
			@include flexLayout(center, center);
			flex-direction: column;

			.pageTitleImg {
				@include setwh(222upx, 40upx);
				margin-bottom: 6upx;

			}

		}
	}

	.cardWrap {
		width: 100%;
		// min-height: calc(100vh - 406upx);
		// position: relative;
		// top: -48upx;
		background: #FFFFFF;
		padding-top: 68upx;
		padding-bottom: 200upx;
		// box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0,0,0,0.5);
		border-radius: 30rpx 30rpx 0rpx 0rpx;
		.title{
			padding-left: 86upx;
			padding-bottom: 66upx;
			font-family: AlimamaFangYuanTiVF, AlimamaFangYuanTiVF;
			font-weight: 500;
			font-size: 46rpx;
			color: #656565;
		}
		.pictureBox {
			// width: 600upx;
			margin-left: 20upx;
			@include flexLayout(center, flex-start);
			flex-wrap: wrap;
			column-gap: 8rpx;
			row-gap: 28upx;
			// background: #FFFFFF;
			position: relative;
			z-index: 3;

			.optionLay {
				position: absolute;
				top: 0;
				left: 0;
				width: 40rpx;
				height: 32rpx;
				background: rgba(0, 0, 0, 0.43);
				border-radius: 0rpx 0rpx 10rpx 0rpx;

				.optionIcon {
					@include positionCenter;
					line-height: 1;
				}
			}

			.infoMore {
				@include flexLayout(center, flex-start);
				padding-top: 11upx;
				min-height: 28upx;

				.priceTxt {
					@include flexLayout(center);
					font-family: SourceHanSansSC, SourceHanSansSC;
					font-weight: bold;
					font-size: 16rpx;
					color: #FFB3B3;

					// line-height: 32rpx;
					.unit {
						font-size: 18rpx;
						// line-height: 1;
					}
				}

				.forward {
					color: #828282;
					font-size: 14rpx;

					.txt {
						margin-right: 6upx;
					}

					.unit {
						font-size: 16rpx;
						// line-height: 1;
					}
				}
			}
		}

	}

	.playWrap {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		width: 750rpx;
		height: 187rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -4rpx 29rpx 4rpx rgba(210, 210, 210, 0.5);
		border-radius: 29rpx 29rpx 0rpx 0rpx;
		@include flexLayout(center, space-between);
		padding: 32upx 58upx 70upx 48upx;
		z-index: 9;

		.all {
			font-family: SourceHanSansSC, SourceHanSansSC;
			font-weight: 500;
			font-size: 24rpx;
			color: #656565;
			line-height: 32rpx;

			.optionIcon {
				margin-right: 18upx;
			}
		}

		.infoBox {
			.infoTxt {
				@include flexLayout(center, flex-start);

				.lable {
					font-family: SourceHanSansSC, SourceHanSansSC;
					font-weight: 500;
					font-size: 24rpx;
					color: #656565;
					line-height: 32rpx;
				}

				.value {
					@include flexLayout(center, center);
					margin-left: 38upx;

					.priceTxt {
						font-family: SourceHanSansSC, SourceHanSansSC;
						font-weight: bold;
						font-size: 32rpx;
						color: #FF7994;
						line-height: 48rpx;

						.unit {
							font-size: 34rpx;
						}

						.delLin {
							position: relative;

							&::before {
								content: '';
								@include positionCenter;
								background-color: #FF7994;
								width: 100%;
								height: 4upx;
							}
						}
					}
				}
			}
		}

		.bnt {
			width: 144rpx;
			height: 56rpx;
			background: #FF7994;
			border-radius: 32rpx;
			font-family: SourceHanSansSC, SourceHanSansSC;
			font-weight: 500;
			font-size: 24rpx;
			color: #FFFFFF;
			line-height: 56rpx;
			text-align: center;
		}
	}
</style>